<?php

namespace DebugComponent\Renderer;

/**
 * Helper class for humanize quantities
 */
abstract class Humanize {

    /**
     * Humanize time quantities
     *
     * @param integer|float $timeInSeconds
     * @return string formatted time value
     */
    public static function time($timeInSeconds) {
        if ($timeInSeconds <= 1 && $timeInSeconds > 0.001) {
            $result = ceil($timeInSeconds * 1000) . 'ms';
        } else if ($timeInSeconds <= 0.001 && $timeInSeconds > 0.00001) {
            $result = ceil($timeInSeconds * 1000000) . 'us';
        } else if ($timeInSeconds <= 0.00001) {
            $result = 'lightly fast';
        } else if ($timeInSeconds > 1 && $timeInSeconds < 60) {
            $result = ceil($timeInSeconds) . 's';
        } else if ($timeInSeconds >= 60 && $timeInSeconds < 3600) {
            if ($timeInSeconds % 60 !== 0) {
                $result = floor($timeInSeconds / 60) . 'm ' . ceil($timeInSeconds % 60) . 's';
            } else {
                $result = floor($timeInSeconds / 60) . 'm';
            }
        } else if ($timeInSeconds >= 3600) {
            if ($timeInSeconds % 3600 !== 0) {
                if ($timeInSeconds % 60 !== 0) {
                    $result = floor($timeInSeconds / 3600) . 'h ' . floor(($timeInSeconds % 3600) / 60) . 'm ' . ceil($timeInSeconds % 60) . 's';
                } else {
                    $result = floor($timeInSeconds / 3600) . 'h ' . floor(($timeInSeconds % 3600) / 60) . 'm';
                }
            } else {
                $result = floor($timeInSeconds / 3600) . 'h';
            }
        }

        return $result;
    }

    /**
     * Humanize capacity quantities
     *
     * @param integer $capacityInBytes
     * @param integer $precision
     * @return string formatted capacity value
     */
    public static function capacity($capacityInBytes, $precision=2) {
        $extensions = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');
        $extension = 0;
        while (($capacityInBytes / 1024) > 1) {
            $capacityInBytes = $capacityInBytes / 1024;
            $extension++;
        }
        return round($capacityInBytes, $precision) . $extensions[$extension];
    }
}